package com.whl.sm.service.impl.notice;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.notice.CommunityNoticeMapper;
import com.whl.sm.model.notice.CommunityNotice;
import com.whl.sm.model.notice.UserCpNotice;
import com.whl.sm.model.system.UserCpRole;
import com.whl.sm.service.notice.CommunityNoticeService;
import com.whl.sm.service.notice.UserCpNoticeService;
import com.whl.sm.service.system.OrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-17
 */
@Service
public class CommunityNoticeServiceImpl extends ServiceImpl<CommunityNoticeMapper, CommunityNotice> implements CommunityNoticeService {

    @Autowired
    private UserCpNoticeService userCpNoticeService;

    @Autowired
    private OrgUserService orgUserService;

    @Override
    public Result update(CommunityNotice communityNotice) {
        try {
            communityNotice.setOperator(orgUserService.getCurrentUserId());
            if (Objects.isNull(communityNotice.getId())) {
                getBaseMapper().insert(communityNotice);
            } else {
                if (communityNotice.getVisible() == 0){
                    communityNotice.setRepealTime(new Date());
                }
                getBaseMapper().updateById(communityNotice);
            }
        } catch (Exception e) {
            log.error("公告管理:公告更新异常" + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public Result publish(CommunityNotice communityNotice) {
        try{
            String currentId = communityNotice.getId();
            Result updateResult = update(communityNotice);
            if (!updateResult.isOk()){
                return updateResult;
            }
            if (Objects.isNull(currentId) && communityNotice.getObjectType() != 3
            ){
                Result sendNotice = new Result();
                if (communityNotice.getObjectType() == 2){
                    sendNotice = userCpNoticeService.publishDepRole(communityNotice.getId(),communityNotice.getRoleTypeId());
                }else if (communityNotice.getObjectType() == 1){
                    sendNotice = userCpNoticeService.publishDepUserList(communityNotice.getId(),communityNotice.getUserIdList());
                }
                if (!sendNotice.isOk()){
                    return sendNotice;
                }
            }
        }catch (Exception e){
            log.error("公告发布异常" + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    public Result buildPersonalNotice(List<String> linkUser,String title,String content) {
        try {
            CommunityNotice insert = new CommunityNotice();
            insert.setNoticeTitle(title);
            insert.setNoticeContent(content);
            insert.setObjectType(1);
            Result insertNotice = this.update(insert);
            if (!insertNotice.isOk()){
                return Result.fail();
            }
            for (String userId :linkUser){
                UserCpNotice insertUN = new UserCpNotice();
                insertUN.setNoticeId(insert.getId());
                insertUN.setUserId(userId);
                Result uNInsert = userCpNoticeService.update(insertUN);
                if (!uNInsert.isOk()){
                    return Result.fail();
                }
            }
            return Result.ok();
        }catch (Exception e){
            log.error("公告发个人公告生成失败======>" + e.getMessage());
            return Result.fail();
        }
    }


}
